package DomainLayer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import Annotations.AttributeNameAnnotation;
import Annotations.EntityNameAnnotation;


@EntityNameAnnotation(Name = "Review")
public class ReviewEntity extends AbstractEntity implements ICommentable, IRatingSupportable {

    @AttributeNameAnnotation(Name = "TextReview", isAggregator = false)
    private String text;

    @AttributeNameAnnotation(Name = "Author", isAggregator = true)
    private UserEntity author;

    @AttributeNameAnnotation(Name = "Rating", isAggregator = false)
    private int rating = 0;

    @AttributeNameAnnotation(Name = "Comments", isAggregator = true, linksTable = "ReviewCom_Link_Table", sourceTableName = "Commentary", genericParam = "Commentary")
    private ArrayList<CommentaryEntity> comments;

    public void setRating(int rating) {
        this.rating = rating;
    }

    public int getRating() {
        return rating;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

     public UserEntity getAuthor() {
        return author;
    }

    public void setAuthor(UserEntity user) {
            this.author = user;
    }

    @Override
    public CommentaryEntity createCommentary(String comment, UserEntity user) {
        CommentaryEntity commentary = new CommentaryEntity(user, null, comment);
        comments.add(commentary);
        return commentary;
    }

    @Override
    public void removeCommentary(int id) {
        for (Iterator<CommentaryEntity> it = comments.iterator(); it.hasNext();) {
            if (it.next().getId().equals(id)) {
                it.remove();
                break;
            }
        }
    }

    @Override
    public List<CommentaryEntity> getCommentaries() {
        return comments;
    }
}